package com.example.lxs_demo1.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.viewpager.widget.ViewPager;


import com.example.lxs_demo1.R;
import com.example.lxs_demo1.adapter.Viewpager_Adapter;

import java.util.ArrayList;
import java.util.List;

public class FirstPager extends Activity {

    private ViewPager viewPager;
    private View view1,view2,view3;
    private List<View> list;
    private Button button;
    private ImageView[] imageViews;
    private int[] ids={R.id.img1,R.id.img2,R.id.img3};
    private int isview=0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        isFirstload();
        setContentView(R.layout.viewpager);
        viewPager = findViewById(R.id.view_pager);
        button = findViewById(R.id.go);
        button.setVisibility(View.GONE);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(getApplicationContext(), "即将进入下一页", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(FirstPager.this, LoginActivity.class));
            }
        });
        LayoutInflater layoutInflater = getLayoutInflater();
        view1 = layoutInflater.inflate(R.layout.view1, null);
        view2 = layoutInflater.inflate(R.layout.view2, null);
        view3 = layoutInflater.inflate(R.layout.view3, null);

        list = new ArrayList<>();
        list.add(view1);
        list.add(view2);
        list.add(view3);
        viewPager.setAdapter(new Viewpager_Adapter(list));
        imageViews = new ImageView[list.size()];
        for (int i = 0; i < imageViews.length; i++) {
            imageViews[i] = findViewById(ids[i]);
        }
        imageViews[0].setImageResource(R.drawable.bc_green);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        button.setVisibility(View.GONE);
                        imageViews[0].setImageResource(R.drawable.bc_green);
                        imageViews[1].setImageResource(R.drawable.bc_green_scoket);
                        imageViews[2].setImageResource(R.drawable.bc_green_scoket);
                        break;
                    case 1:
                        button.setVisibility(View.GONE);
                        imageViews[1].setImageResource(R.drawable.bc_green);
                        imageViews[0].setImageResource(R.drawable.bc_green_scoket);
                        imageViews[2].setImageResource(R.drawable.bc_green_scoket);
                        break;
                    case 2:
                        imageViews[0].setImageResource(R.drawable.bc_green_scoket);
                        imageViews[1].setImageResource(R.drawable.bc_green_scoket);
                        imageViews[2].setImageResource(R.drawable.bc_green);
                        button.setVisibility(View.VISIBLE);
                        break;
                   default:
                       imageViews[0].setImageResource(R.drawable.bc_green_scoket);
                       imageViews[1].setImageResource(R.drawable.bc_green_scoket);
                       imageViews[2].setImageResource(R.drawable.bc_green_scoket);
                       button.setVisibility(View.VISIBLE);
                       break;
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }
    private void isFirstload() {
        SharedPreferences sharedPreferences = getSharedPreferences("isfirst", MODE_PRIVATE);
        boolean flag=sharedPreferences.getBoolean("flag",false);
        if(flag)
            startActivity(new Intent(FirstPager.this,LoginActivity.class));
        else {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("flag", true);
            editor.commit();
        }
    }
}
